//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //imgUrl
    imgUrl: app.globalData.iconImgUrl,
    backImg: app.globalData.iconImgUrl+"arrow-left.svg",
    noAgreeVisible: false,
    type: 'self',
    current: 'tab1',
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    backIcon: true,
    title: "审核详情",
    value: '',
    noAgreeScVisible: false,
    imgArr: [],
    hdTabObj: [{
        name: "为自己申请",
        active: "active"
      },
      {
        name: "为他人申请",
        active: ""
      }
    ],
    pickIndex: 0,
    addScodeIndex: 0,
    addScodeVal: '+10',
    nameIndex: 0,
    markType: ''
  },
  // 查看大图
  lookBigPic(e) {
    var url = e.currentTarget.dataset.url;
    wx.previewImage({
      current: '', // 当前显示图片的http链接
      urls: [url] // 需要预览的图片http链接列表
    })
  },
  showModal(modalName) {
    this.setData({
      [modalName]: true
    })
  },
  hideModal(modalName) {
    this.setData({
      [modalName]: false
    })
  },
  hideApply() {
    this.hideModal("modalVisible")
    wx.navigateBack()
  },
  agree() {
    const id = this.data.id
    const that = this
    var data = {
      "id": id,
      "type": "agree",
      "userCode": wx.getStorageSync('userCode'),
      "reson": ""
    }
    wx.request({
      url: app.globalData.baseUrl + 'application/audit.do',
      method: 'post',
      data: data,
      success(res) {
        console.log(res)
        if (res.data.rstype) {
          that.showModal("modalVisible")
        } else {
          wx.showToast({
            title: res.data.rsmsg,
            image: '/assets/images/icon/error.png'
          })
        }

      },
      fail(error) {
        wx.showToast({
          title: '错误',
        })
      }
    })
  },
  noAgree() {
    const roleId = wx.getStorageSync('roleId')
    console.log(roleId)
    if (roleId !== '24' || roleId !== '25') {
      this.showModal("noAgreeVisible")
    } else {
      this.showToast({
        title: '无权限审批',
        image: '/assets/images/icon/error.png'
      })
    }

  },
  cancel() {
    this.hideModal("noAgreeVisible")
  },
  hideAgree() {
    const id = this.data.id
    const that = this
    var data = {
      "id": id,
      "type": "noAgree",
      "userCode": wx.getStorageSync('userCode'),
      "reson": that.data.value
    }
    console.log('参数', data)
    wx.request({
      url: app.globalData.baseUrl + 'application/audit.do',
      method: 'post',
      data: data,
      success(res) {

        if (res.data.rstype) {
          wx.showToast({
            title: '提交成功',
            success() {
              setTimeout(function() {
                wx.navigateBack()
              }, 500)
            }
          })
        } else {
          wx.showToast({
            title: res.data.rsmsg,
            image: '/assets/images/icon/error.png'
          })
        }
        that.hideModal("noAgreeVisible");
      }
    })
  },
  hideNoAgreeSc() {
    this.hideModal("noAgreeScVisible")
  },
  noAgreeInput(e) {
    console.log(e.detail.value)
    this.setData({
      value: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // console.log(options)
    this.setData({
      id: options.id
    })
    this.getDetail(options.id)
  },
  getDetail(id) {
    var that = this
    wx.request({
      url: app.globalData.baseUrl + 'application/auditDesc.do?id=' + id,
      data: {
        id: id,
        userCode: wx.getStorageSync("userCode")
      },
      method: 'post',
      success(res) {
        console.log(res)

        if (res.statusCode === 200) {
          const data = res.data.application
          const markType = data.markType
          var addtions = {}
          var type = data.type,
            subType = data.subType,
            typeItem,
            subTypeTemp,
            typeItem2;

          if (type === 'self') {
            typeItem = "hdTabObj[0].active"
            typeItem2 = "hdTabObj[1].active"
          } else {
            typeItem2 = "hdTabObj[0].active"
            typeItem = "hdTabObj[1].active"
          }

          switch (subType) {
            case 'integral':
              subTypeTemp = 'tab1'
              break;
            case 'honor':
              subTypeTemp = 'tab2'
              break;
            case 'dreamPoint':
              subTypeTemp = 'tab3'
              break;
            default:
              break;
          }

          // 判断当前是为固定还是其它还是加班
          switch (markType) {
            case "regular":
              break;
            case 'other':
              break;
            case "overTime":
              that.setData({
                beginTime: data.beginTime,
                endTime: data.endTime
              })
              break;
            default:
              break;
          }

          var imgArr = data.imgArr;

          function empty(item) {
            var empty = (item === null || item === '' || item === [] || item === 'undefined')
            return empty
          }
          if (imgArr.every(empty)) {
            imgArr.length = 0
          }

          //是否同意
          if (data.result) {
            that.setData({
              result: data.result
            })
          }

          that.setData({
            [typeItem2]: '',
            [typeItem]: 'active',
            imgArr: imgArr,
            current: subTypeTemp,
            introduce: data.Introduce,
            markType: data.markType,
            markName: data.markName,
            mark: data.mark,
            reson: data.reason,
            type: data.type,
            personArr: data.personArr
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function(option) {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})